package com.xian.hadoop.mr;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Iterator;

//汇总
public class MyReducer extends Reducer<Text, IntWritable,Text,IntWritable> {

    private IntWritable count = new IntWritable();

    //同一个key调用一次reduce
    @Override
    protected void reduce(Text key, Iterable<IntWritable> ones, Context context) throws IOException, InterruptedException {
        int sum = 0; //汇总相同key出现次数
        Iterator<IntWritable> iterator = ones.iterator();
        while (iterator.hasNext()) {
            int one = iterator.next().get();
            sum += one;
        }
        context.write(new Text(key), new IntWritable(sum));
    }
}
